package com.gitlab.bootren.data.core.query.restriction;

/**
 * 条件约束枚举
 *
 * @author tengda
 */
public enum RestrictionType implements IQLRestrictionType, java.io.Serializable {

    ISNULL("is null") {
        @Override
        public String toQl(String key, Object value, String paramName) {
            return "this." + key + " is NULL";
        }
    },
    NOTNULL("is not null"){
        @Override
        public String toQl(String key, Object value, String paramName) {
            return "this." + key + " is not NULL";
        }
    },
    EQ("=") {
        @Override
        public String toQl(String key, Object value, String paramName) {

            if (value == null) {
                return "";
            }

            return "this." + key + "=:" + paramName;

        }
    },
    NQ("<>") {
        @Override
        public String toQl(String key, Object value, String paramName) {

            if (value == null) {
                return "";
            }

            return "this." + key + " <> :" + paramName;
        }

    },
    LIKE("like") {
        @Override
        public String toQl(String key, Object value, String paramName) {
            if (value == null) {
                return "";
            }
            return "this." + key + " LIKE :" + paramName;
        }
    },
    GT(">") {
        @Override
        public String toQl(String key, Object value, String paramName) {
            if (value == null) {
                return "";
            }
            return "this." + key + " >  :" + paramName;
        }

    },
    GE(">=") {
        @Override
        public String toQl(String key, Object value, String paramName) {
            if (value == null) {
                return "";
            }
            return "this." + key + " >=  :" + paramName;
        }

    },
    LE("<=") {
        @Override
        public String toQl(String key, Object value, String paramName) {
            if (value == null) {
                return "";
            }
            return "this." + key + " <=  :" + paramName;
        }

    },
    LT("<") {
        @Override
        public String toQl(String key, Object value, String paramName) {
            if (value == null) {
                return "";
            }
            return "this." + key + " < :" + paramName;
        }

    }, IN("in") {
        @Override
        public String toQl(String key, Object value, String paramName) {

            if (value == null) {
                return "";
            }
            return "this." + key + " in :" + paramName;
        }
    }, NIN("not in") {
        @Override
        public String toQl(String key, Object value, String paramName) {

            if (value == null) {
                return "";
            }
            return "this." + key + " not in :" + paramName;
        }
    };

    private String name;

    public String getName() {
        return name;
    }

    private RestrictionType(String name) {
        this.name = name;
    }

    private static String fromatParamName(String paramName) {
        return paramName.replaceAll("\\.", "_");
    }

    public static RestrictionType getRestrictionTypeByName(String name) {

        RestrictionType[] ts = RestrictionType.class.getEnumConstants();
        for (RestrictionType t : ts) {
            if (t.toString().equalsIgnoreCase(name)) {
                return t;
            }
        }
        return RestrictionType.EQ;

    }

}
